package com.demo.mybatis;

import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import org.apache.ibatis.reflection.MetaObject;
import org.springframework.stereotype.Component;

import java.util.Date;

/**
 * 公共字段填充配置类
 *
 * @author zm
 */
@Component
public class MetaObjectHandlerConfig implements MetaObjectHandler {

    /**
     * 新增数据执行
     *
     * @param metaObject
     */
    @Override
    public void insertFill(MetaObject metaObject) {
        if (metaObject.hasGetter("createdTime") && metaObject.getValue("createdTime") == null) {
            setFieldValByName("createdTime", new Date(), metaObject);
        }
    }

    /**
     * 更新数据执行
     *
     * @param metaObject
     */
    @Override
    public void updateFill(MetaObject metaObject) {
        setUpdateFieldValByName("lastModifyTime", new Date(), metaObject);
        setUpdateFieldValByName("stateTime", new Date(), metaObject);
        setFieldValByName("updatedTime", new Date(), metaObject);
    }

}
